package com.micro.frog.rbac.controller;

import com.micro.frog.common.base.PageRes;
import com.micro.frog.common.base.Result;
import com.micro.frog.common.utils.BeanCopyUtil;
import com.micro.frog.rbac.entity.DictDO;
import com.micro.frog.rbac.service.DictService;
import com.micro.frog.rbac.vo.DictVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :  ljh
 * @date : 2018/4/11
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/rbac/dict")
public class DictController {

    private final DictService dictService;

    /**
     * 带条件查询带分页
     */
    @GetMapping(value = "/page")
    public Result<PageRes<DictDO>> page(DictVO req) {
        return dictService.queryPage(req);
    }

    /**
     * 查询全部不带分页
     */
    @GetMapping(value = "/list")
    public Result list(DictVO req) {
        DictDO dict = new DictDO();
        BeanCopyUtil.copy(req, dict);
        return Result.success(dictService.queryList(dict));
    }

    @PostMapping("/add")
    public Result add(DictDO dict) {
        boolean flag = dictService.save(dict);
        return flag ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PostMapping("/delete")
    public Result deleteDict(Long id) {
        boolean flag = dictService.delete(id);
        return flag ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("/update")
    public Result update(DictDO dict) {
        boolean flag = dictService.update(dict);
        return flag ? Result.success("更新成功") : Result.error("更新失败");
    }

}
